//
//  HEEduTitleCell.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/12/17.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEEduTitleCell.h"
#import "HEEduVideoModel.h"

@interface HEEduTitleCell ()

@property (nonatomic, strong) UILabel *titleLb;
@property (nonatomic, strong) UILabel *numLb;
@property (nonatomic, strong) UILabel *descLb;
@property (strong, nonatomic) QMUIButton *foldBtn;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation HEEduTitleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
    }
    return self;
}

- (void)initUI {
     

    _titleLb = [UILabel new];
    _titleLb.text = @"-";
    _titleLb.font = DYBlodFont(17);
    _titleLb.numberOfLines = 0;
    _titleLb.textColor = CNavLightBlackColor;
    [self.contentView addSubview:_titleLb];
    
    _numLb = [UILabel new];
    _numLb.text = @"-";
    _numLb.font = DYFont(15);
    _numLb.textColor = CNavLightBlackColor;
    [self.contentView addSubview:_numLb];
    
    _descLb = [UILabel new];
    _descLb.text = @"-";
    _descLb.font = DYFont(17);
    _descLb.numberOfLines = 1;
    _descLb.textColor = CNavLightBlackColor;
    [self.contentView addSubview:_descLb];
    
    QMUIButton *check = [[QMUIButton alloc] init];
    check.imagePosition = QMUIButtonImagePositionRight;
    check.tintColorAdjustsTitleAndImage = CNavSubTitleColor;
    check.adjustsImageTintColorAutomatically = NO;
    check.spacingBetweenImageAndTitle = 5;
    [check setTitle:@"简介" forState:UIControlStateNormal];
    [check setTitleColor:CNavDargGrayColor forState:UIControlStateNormal];
    [check setImage:UIImageMake(@"item_close") forState:UIControlStateSelected];
    [check setImage:UIImageMake(@"item_expand") forState:UIControlStateNormal];
    [check setImage:UIImageMake(@"item_expand") forState:UIControlStateDisabled];

    check.titleLabel.font = DYFont(15);
    [self.contentView addSubview:check];
    _foldBtn = check;
    [check addTarget:self action:@selector(foldClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [check mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.width.offset([@"简介" widthForFont: DYFont(15)] + 20);
    }];
    
    [_titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(15);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(check.mas_left).offset(-15);
    }];
    
    [_numLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLb.mas_bottom).offset(10);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
    }];
    
    [_descLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_numLb.mas_bottom).offset(10);
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.bottom.equalTo(self.contentView).offset(-15);
    }];
}

- (void)foldClick:(QMUIButton *)sender {
    sender.selected = !sender.selected;
    
//    [self.tableView.superview setNeedsUpdateConstraints];
//    [self.tableView.superview updateConstraintsIfNeeded];
 
    if (sender.selected) {
        _descLb.numberOfLines = 0;
//        [_descLb mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(_numLb.mas_bottom).offset(10);
//            make.left.equalTo(self.contentView).offset(15);
//            make.right.equalTo(self.contentView).offset(-15);
//            make.height.mas_equalTo(15);
//            make.bottom.equalTo(self.contentView).offset(-15);
//        }];
    }else {
        _descLb.numberOfLines = 1;
//        [_descLb mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(_numLb.mas_bottom).offset(10);
//            make.left.equalTo(self.contentView).offset(15);
//            make.right.equalTo(self.contentView).offset(-15);
//            make.bottom.equalTo(self.contentView).offset(-15);
//        }];
    }
    
    [self refreshCellHeight];
    [self.tableView reloadData];
}

- (void)configCellWithModel:(id)model indexPath:(NSIndexPath *)indexPath{
    HEEduVideoModel *item = (HEEduVideoModel *)model;
    _titleLb.text = item.video.firstObject.title;
    _numLb.text = [NSString stringWithFormat:@"全%ld集 - %ld次播放", item.video.count, item.viedo_cnt];
    _descLb.text = item.brief;
}

- (void)refreshCellHeight {
////    [self.tableView.fd_indexPathHeightCache invalidateHeightAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//    [self.tableView.fd_indexPathHeightCache invalidateAllHeightCache];

    [self.tableView beginUpdates];
    [self.tableView endUpdates];
    
//    [self.tableView.superview layoutIfNeeded];

}

- (UITableView *)tableView {
    if (!_tableView) {
        // 寻找Cell所属的tableView
        UIView *tableView = self.superview;
        while (![tableView isKindOfClass:[UITableView class]] && tableView) {
            tableView = tableView.superview;
        }
        _tableView = (UITableView *)tableView;
    }
    return _tableView;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
